﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    m_scene = new QGraphicsScene(this);
    m_scene->setSceneRect(-200,-100,400,200);
    ui->graphicsView->setScene(m_scene);
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);

    connect(ui->graphicsView,SIGNAL(mouseClicked(QPointF)),this,SLOT(onMouseClicked(QPointF)));
}




void MainWindow::on_actLine_triggered(bool checked)
{
    if(checked == true){
        m_points.clear();
        ui->actLines->setChecked(false);
        ui->actPolyline->setChecked(false);
    }else{
        if(m_points.count() == 2){

            QGraphicsLineItem* item = new QGraphicsLineItem(m_points.at(0).x(),m_points.at(0).y(),m_points.at(1).x(),m_points.at(1).y());
            item->setFlags(QGraphicsItem::ItemIsFocusable |
                           QGraphicsItem::ItemIsSelectable
                           );
            QPen pen;
            pen.setColor("blue");
            pen.setWidth(5);
            item->setPen(pen);
            m_scene->addItem(item);
            m_scene->clearSelection();
            item->setSelected(true);




        }
        m_points.clear();
    }
}

void MainWindow::onMouseClicked(QPointF p)
{
    m_points.append(ui->graphicsView->mapToScene(p.toPoint()));
}

void MainWindow::on_actLines_triggered(bool checked)
{
    if(checked == true){
        m_points.clear();
        ui->actLine->setChecked(false);
        ui->actPolyline->setChecked(false);
    }else{
        if(m_points.count() > 2){

            QGraphicsPathItem* item = new QGraphicsPathItem();
            item->setFlags(QGraphicsItem::ItemIsFocusable |
                           QGraphicsItem::ItemIsSelectable
                           );

            QPen pen;
            pen.setColor("red");
            pen.setWidth(5);
            item->setPen(pen);
            QPainterPath path(m_points.at(0));

            for(int i = 1;i < m_points.count();++i){
                path.lineTo(m_points.at(i));
            }
            item->setPath(path);
            m_scene->addItem(item);
            m_scene->clearSelection();
//            item->setSelected(true);


            m_points.clear();
        }

    }
}


void MainWindow::on_actEllipse_triggered()
{
    QGraphicsEllipseItem* item = new QGraphicsEllipseItem(-50,-30,100,60);
    item->setFlags(QGraphicsItem::ItemIsFocusable |
                   QGraphicsItem::ItemIsSelectable
                   );
    item->setBrush(QBrush("gray"));
    item->setPos(-100+QRandomGenerator(QTime::currentTime().second()).bounded(100),-60+QRandomGenerator(QTime::currentTime().second()).bounded(100));



    m_scene->addItem(item);
    m_scene->clearSelection();
    item->setSelected(true);
}


void MainWindow::on_actPolyline_triggered(bool checked)
{

    if(checked == true){
        m_points.clear();
        ui->actLine->setChecked(false);
        ui->actLines->setChecked(false);
    }else{
        if(m_points.count() >= 3){

            QGraphicsPolygonItem* item = new QGraphicsPolygonItem;

            QPolygonF points;
            for(int i = 0;i < m_points.count(); ++i){
                points.append(m_points.at(i));
            }
            item->setPolygon(points);
            item->setFlags(QGraphicsItem::ItemIsFocusable |
                           QGraphicsItem::ItemIsSelectable
                           );
            item->setBrush(QBrush("yellow"));

            m_scene->addItem(item);
            m_scene->clearSelection();
            item->setSelected(true);



            m_points.clear();
        }

    }

}


void MainWindow::on_actMove_triggered()
{
    QList<QGraphicsItem*> items =  m_scene->selectedItems();
    if(items.count() <= 0){
        QMessageBox::warning(0,"警告","没有选中的项");
        return;
    }
    double x = 0;
    x = QInputDialog::getDouble(0,"输入X位移","请输入X的位移值:");
    double y = 0;
    y = QInputDialog::getDouble(0,"输入Y位移","请输入Y的位移值:");
    QGraphicsItem* item = items.at(0);
    item->setPos(item->pos().x() + x,item->pos().y()+y);
}


void MainWindow::on_actColor_triggered()
{
    QList<QGraphicsItem*> items =  m_scene->selectedItems();
    if(items.count() <= 0){
        QMessageBox::warning(0,"警告","没有选中的项");
        return;
    }
    QGraphicsItem* item = items.at(0);

    if(item->type() == QGraphicsLineItem::Type){
        QGraphicsLineItem* item1 = (QGraphicsLineItem*)item;
        QPen pen(item1->pen());

        QColor color = QColorDialog::getColor(pen.color(),0,"选择颜色");
        if(color.isValid()){

            pen.setColor(color);
            item1->setPen(pen);
        }
    }else if(item->type() == QGraphicsPathItem::Type){
        QGraphicsPathItem* item1 = (QGraphicsPathItem*)item;
        QPen pen(item1->pen());
        QColor color = QColorDialog::getColor(pen.color(),0,"选择颜色");
        if(color.isValid()){

            pen.setColor(color);
            item1->setPen(pen);
        }
    }else if(item->type() == QGraphicsEllipseItem::Type){
        QGraphicsEllipseItem* item1 = (QGraphicsEllipseItem*)item;
        QBrush brush(item1->brush());
        QColor color = QColorDialog::getColor(brush.color(),0,"选择颜色");
        if(color.isValid()){

            brush.setColor(color);
            item1->setBrush(brush);
        }
    }else if(item->type() == QGraphicsPolygonItem::Type){
        QGraphicsPolygonItem* item1 = (QGraphicsPolygonItem*)item;
        QBrush brush(item1->brush());
        QColor color = QColorDialog::getColor(brush.color(),0,"选择颜色");
        if(color.isValid()){

            brush.setColor(color);
            item1->setBrush(brush);
        }
    }
}


void MainWindow::on_actProperty_triggered()
{
    QList<QGraphicsItem*> items =  m_scene->selectedItems();
    if(items.count() <= 0){
        QMessageBox::warning(0,"警告","没有选中的项");
        return;
    }
    QGraphicsItem* item = items.at(0);

    if(item->type() == QGraphicsLineItem::Type){
        QGraphicsLineItem* item1 = (QGraphicsLineItem*)item;
        QMessageBox::information(0,"提示","线段长度:"+QString("%1").arg(item1->line().length()));
    }else if(item->type() == QGraphicsPathItem::Type){
        QGraphicsPathItem* item1 = (QGraphicsPathItem*)item;
        QMessageBox::information(0,"提示","多线段长度:"+QString("%1").arg(item1->path().length()));
    }else if(item->type() == QGraphicsEllipseItem::Type){
        QGraphicsEllipseItem* item1 = (QGraphicsEllipseItem*)item;
        QMessageBox::information(0,"提示","图形面积:"+QString("%1").arg((item1->rect().height()/2) * (item1->rect().width()/2) * 3.14));
    }else if(item->type() == QGraphicsPolygonItem::Type){
        QGraphicsPolygonItem* item1 = (QGraphicsPolygonItem*)item;
        double s;
        for(int i = 0; i < item1->polygon().count();++i){
            s = item1->polygon()[0].y() * (item1->polygon()[item1->polygon().count() - 1].x() - item1->polygon()[1].x());
            for(int i = 1; i < item1->polygon().count(); ++i) {
                s += item1->polygon()[i].y() * (item1->polygon()[i - 1].x() - item1->polygon()[(i + 1) % item1->polygon().count()].x());
            }
        }
        QMessageBox::information(0,"提示","图形面积:"+QString("%1").arg(fabs(s/2.0)));
    }
}


void MainWindow::on_actNew_triggered()
{
    m_scene->clear();

}


void MainWindow::on_actSave_triggered()
{
    QString filePath = QFileDialog::getExistingDirectory(0,"选择文件夹",QCoreApplication::applicationDirPath());
    QDir dir(filePath);
    if(!dir.exists()){
        return;
    }
    QFile file(filePath+"\\"+"data.txt");

        file.open(QIODevice::WriteOnly);
        QTextStream stream(&file);

    QList<QGraphicsItem*> items = m_scene->items();
        QGraphicsItem* item;
        for(int i = 0;i < items.count(); ++i){
        item = items.at(i);
        if(item->type() == QGraphicsLineItem::Type){
            QGraphicsLineItem* item1 = (QGraphicsLineItem*)item;

            stream << "line "+item1->pen().color().name()+" "+QString("%1,%2 %3,%4").arg(item1->line().x1()).arg(item1->line().y1()).arg(item1->line().x2()).arg(item1->line().y2())<<"\n";
        }else if(item->type() == QGraphicsPathItem::Type){
            QGraphicsPathItem* item1 = (QGraphicsPathItem*)item;

            stream << "path "+item1->pen().color().name()+" ";
            for(int i = 0;i < item1->path().elementCount();++i){
                stream << item1->path().elementAt(i).x <<","<<item1->path().elementAt(i).y<<" ";

            }
            stream << "\n";
        }else if(item->type() == QGraphicsEllipseItem::Type){
            QGraphicsEllipseItem* item1 = (QGraphicsEllipseItem*)item;
            stream << "ellipse " << item1->brush().color().name() << " " <<item1->pos().x() << ","<<item1->pos().y() <<" "<<item1->rect().width()<<"*"<< item1->rect().height() <<"\n";
        }else if(item->type() == QGraphicsPolygonItem::Type){
            QGraphicsPolygonItem* item1 = (QGraphicsPolygonItem*)item;
            stream << "polygon " << item1->brush().color().name() <<" ";
            for(int i = 0;i < item1->polygon().count();++i){
                stream << item1->polygon().at(i).x() <<","<< item1->polygon().at(i).y() << " ";
            }
            stream << "\n";
        }
        }
        file.close();
}


void MainWindow::on_actOpen_triggered()
{
        QString filePath = QFileDialog::getOpenFileName(0,"选择文件夹",QCoreApplication::applicationDirPath(),"txt文件(*.txt);;所有文件(*.*)");
        QFile file(filePath);
        if(!file.exists()){
            return;
        }

        file.open(QIODevice::ReadOnly);
        QTextStream stream(&file);
        for(;!stream.atEnd();){

            QStringList list = stream.readLine().trimmed().split(" ",Qt::SkipEmptyParts);
            if(list.at(0) == "line"){
            QList<QPointF> points;
            points.append(QPointF(list.at(2).split(",").at(0).toDouble(),list.at(2).split(",").at(1).toDouble()));
            points.append(QPointF(list.at(3).split(",").at(0).toDouble(),list.at(3).split(",").at(1).toDouble()));
            QGraphicsLineItem* item = new QGraphicsLineItem(points.at(0).x(),points.at(0).y(),points.at(1).x(),points.at(1).y());
            item->setFlags(QGraphicsItem::ItemIsFocusable |
                           QGraphicsItem::ItemIsSelectable
                           );
            QPen pen;
            pen.setColor(list.at(1));
            pen.setWidth(5);
            item->setPen(pen);
            m_scene->addItem(item);

            }else if(list.at(0) == "path"){
            QList<QPointF> points;
                for(int i = 2;i < list.count();++i){
                    points.append(QPointF(list.at(i).split(",").at(0).toDouble(),list.at(i).split(",").at(1).toDouble()));
                }

                QGraphicsPathItem* item = new QGraphicsPathItem();
                item->setFlags(QGraphicsItem::ItemIsFocusable |
                               QGraphicsItem::ItemIsSelectable
                               );

                QPen pen;
                pen.setColor(list.at(1));
                pen.setWidth(5);
                item->setPen(pen);
                QPainterPath path(points.at(0));

                for(int i = 1;i < points.count();++i){
                    path.lineTo(points.at(i));
                }
                item->setPath(path);
                m_scene->addItem(item);

            }else if(list.at(0) == "ellipse"){

                QGraphicsEllipseItem* item = new QGraphicsEllipseItem(-50,-30,100,60);
                item->setFlags(QGraphicsItem::ItemIsFocusable |
                               QGraphicsItem::ItemIsSelectable
                               );

                item->setBrush(QBrush("gray"));
                item->setPos(list.at(2).split(",").at(0).toDouble(),list.at(2).split(",").at(1).toDouble());

                m_scene->addItem(item);

            }
            else if(list.at(0) == "polygon"){
                QList<QPointF> points;
                for(int i = 2;i < list.count();++i){
                    points.append(QPointF(list.at(i).split(",").at(0).toDouble(),list.at(i).split(",").at(1).toDouble()));
                }

                QGraphicsPolygonItem* item = new QGraphicsPolygonItem;

                QPolygonF pointsP;
                for(int i = 0;i < points.count(); ++i){
                    pointsP.append(points.at(i));
                }
                item->setPolygon(pointsP);
                item->setFlags(QGraphicsItem::ItemIsFocusable |
                               QGraphicsItem::ItemIsSelectable
                               );


                item->setBrush(QBrush("yellow"));

                m_scene->addItem(item);

            }
        }
}



